package adapter;

import java.io.IOException;
import java.util.ArrayList;

import com.example.traffic_learning.R;

import DTO.SignDTO;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridAdapterTrafficSignSystem extends BaseAdapter{
	private final Activity context;
	private final ArrayList<SignDTO> listItem;
	
	public GridAdapterTrafficSignSystem(Activity context, ArrayList<SignDTO> listItem){
		this.context = context;
		this.listItem = listItem;
	}
	
	@SuppressLint("ViewHolder")
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.traffic_sign_system_grid_item, parent, false);
		TextView textItem = (TextView)rowView.findViewById(R.id.text_traffic_sign_system_grid_item);
		textItem.setText(listItem.get(position).getNumber());
		ImageView image = (ImageView)rowView.findViewById(R.id.image_traffic_sign_system_grid_item);
		Drawable imageDrawable = null;
		try {
			Options opts = new BitmapFactory.Options();
			opts.inDensity = DisplayMetrics.DENSITY_LOW;
			imageDrawable = Drawable.createFromResourceStream(context.getResources(), null, context.getAssets().open(listItem.get(position).getImagePath()), null, opts);
			image.setImageDrawable(imageDrawable);
		} catch (IOException e) {
			DisplayMetrics metrics = new DisplayMetrics();
			context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
			Options opts = new BitmapFactory.Options();
			opts.inDensity = metrics.densityDpi;
			try {
				imageDrawable = Drawable.createFromResourceStream(context.getResources(), null, context.getAssets().open(listItem.get(position).getImagePath()), null, opts);
				image.setImageDrawable(imageDrawable);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			e.printStackTrace();
		}
		
		return rowView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listItem.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}
}
